package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.Record;
	import engine.display.newAttack.vo.FriendVo;
	import engine.flag.ResourcesPath;
	import engine.logic.ChatFilterLogic;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.FaceIconViewUI;
	import engine.util.FilterUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.scroll.text.ChatText;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 * 私聊逻辑 
	 * @author liuel
	 * 
	 */	
	public class WhisperLogic
	{
		private static var	_self			:WhisperLogic;
		private  var _model					:ModelLocator;
		private  var _bindManager		    	:BindManage;
		public  var ui                    	:WhisperUI;
		private var _currCahtVo          	 	:FriendVo ;	
		private var _viewBox               	:FaceIconViewUI;
		private var 		_friendBack     :int      = -1;
		private var 		_blackListBack  :int      = -1;
		//已读聊天消息(name msg)
		private var _readMsgList				:Array       =[];
		//未读聊天消息
		private var _noReadMsgList				:Array		=[];
		//未读的在线消息
		private var _noReadOnLion				:Array		= [];
		
		//私聊者的信息
		private var dataArr                	:Array;
		//是否有离线消息
		public var  isHaveMsg					:Boolean = false;
		//有在线好友消息
		public var  isOtherMsg					:Boolean = false;
		
		//当前的信息个位置
		private var  _currIndex				:int =0;
		//离线消息用户数组
		private var  _msgNameArr				:Array	=[];
		//在线消息用户数组
		private var  _onlinmsgNameArr			:Array	=[];
		private var  _date						:Date;
		private var  _nowdate					:Date;
		
		private var _acterui					:ActorPropertyUI;
		public static var enterobj					:Object;
		
		public static var closeModel:int=0;
		/**
		 *所有本次上线的聊天记录，一下线就没了 
		 */		
		private var _allChatAnnal:Array =[];
		public static function getInstance():WhisperLogic
		{
			if(_self == null)
			{
				_self = new WhisperLogic();
			}
			return _self;
		}
		public function get UI():FaceIconViewUI
		{
			if(_viewBox ==null)
			{
				_viewBox = new FaceIconViewUI();
				
			}
			return _viewBox;
		}
		public function WhisperLogic()
		{
			ui=new WhisperUI(this);
		}
		public function get whisperUi():WhisperUI
		{
			return ui as WhisperUI;
		}
		public function Init():void
		{
			_nowdate	= new Date();
			_date		= new Date();
			_model        = ModelLocator.getInstance();
			_bindManager =BindManage.getInstance();
			_acterui	= ActorPropertyLogic.getInstance().ui;
			//接受服务器发送的消息
			_model   .addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_WHISPER),getMsg);
			//监听有无离线消息
			_model	 .addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_PROMPT_OFFLINE_WHISPER),getIsHaveMsg)
			//打开私聊界面 （不是从好友面板打开）
			_model	 .addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHAT_PLAYER_INFO),whisperShow);
			//			ui            =WhisperUI.getInstance();
			ui.chatText  = new ChatText();
			ui.inputTxt1 =new TextField();
			initUI();
			
		}
		
		public function	BeforeShow():void
		{
			ui.minBtn.visible=true;
			if(_friendBack == -1)
			{
				_friendBack   = _bindManager.BindRecord(_model.scene,"Friend",addFirend,removeFriend);
			}
			if(_blackListBack == -1)
			{
				_blackListBack = _bindManager.BindRecord(_model.scene,"BlackList",addBlack,removeBlack);
			}
			ui.windowBG.titleIcon="WhisperUIPic"
			updateUI();
			addListener();
			updateFriend();
			updateBlack();
			
		}
		private function check(name:String,record:Record):Boolean
		{
			for(var i:int =0;i<record.recArr.length;i++ )
			{
				
				if(record.recArr.hasOwnProperty(i)&&record.recArr[i][0] ==name)
				{
					return true;
				}
			} 
			return false;
		}
		private function addFirend(PropertiesName:String,val:*,data:*):void
		{
			updateFriend();
		}
		private function removeFriend(PropertiesName:String,PropertiesValue:*,data:*):void
		{
			updateFriend();
		}
		//添加黑名单
		private function addBlack(PropertiesName:String,val:*,data:*):void
		{
			updateBlack();
		}
		//删除黑名单
		private function removeBlack(PropertiesName:String,PropertiesValue:*,data:*):void
		{
			updateBlack();
		}
		private function initUI():void
		{
			_currCahtVo			 = new FriendVo("",0,0,0,0);
			_viewBox		     = new FaceIconViewUI();
			_viewBox.showBtn     = ui.picBtn as DisplayObject;
			_viewBox.callBackFun = viewBoxFunction;
			
			//输出文本
			
			ui.chatText.x          = ui.outputArea.x+8;
			ui.chatText.y          = ui.outputArea.y+8;
			ui.chatText.scrollBarPos =ui.outputArea.width-25;
			ui.chatText.setSizeWH(ui.outputArea.width-25,ui.outputArea.height-10);
			//输入文本
			ui.inputTxt1.x        = ui.inputArea.x+8;
			ui.inputTxt1.y        = ui.inputArea.y+2;
			ui.inputTxt1.maxChars = 60;
			ui.inputTxt1.width    = ui.inputArea.width-10
			ui.inputTxt1.height   = ui.inputArea.height-2;
			ui.inputTxt1.type     = TextFieldType.INPUT;
			ui.inputTxt1.wordWrap = true;
			ui.inputTxt1.textColor= 0xffffff;
			ui.inputTxt1.text     = "";
			if(dataArr!=null)
			{
				ui.picClip.source="res/photos/head_"+ dataArr[10] + ".png";
			}
			
			ui.windowBG  .addChild(ui.chatText);
			ui.windowBG  .addChild(ui.inputTxt1);
			
			ui.inputTxt1.visible  = true;
			
		}
		public function	AfterHide():void
		{
			if(_friendBack != -1)
			{
				_friendBack    = _bindManager.UnbindRecord("Friend",_friendBack);
			}
			if(_blackListBack != -1)
			{
				_blackListBack = _bindManager.UnbindRecord("BlackList",_blackListBack);
			}
			if(this._viewBox.created)
			{
				_viewBox.removeStageEvent();
				_viewBox.hide();
			}
			ui.inputTxt1.text="";
			removeListener();
		}
		private function addListener():void
		{  
			ui.windowBG .addEventListener(NxWindowEvent.WINDOW_CLOSE,                   onClickClose);
			ui.nextBtn	.addEventListener(MouseEvent.CLICK,								onClickNext);
			ui.addBtn	.addEventListener(MouseEvent.CLICK,								onClickAdd);
			ui.sendBtn  .addEventListener(MouseEvent.CLICK,                             onClicksend);
			ui.picBtn   .addEventListener(MouseEvent.CLICK,                        onPicBox);
			
			ui.inputTxt1.addEventListener(Event.CHANGE,								onChange);
			ui.inputTxt1.addEventListener(MouseEvent.CLICK,								onClickInput);
			
			ui.addblackBtn.addEventListener(MouseEvent.CLICK,						onClickAddBlack);
			//			ui.lookBtn	.addEventListener(MouseEvent.CLICK,							onClikckLook);
			StageInfo.stage.addEventListener(KeyboardEvent.KEY_DOWN,                    onEnter);
			
			ui.minBtn.addEventListener(MouseEvent.CLICK,onMinHandle);
			
		}
		private function removeListener():void
		{
			ui.windowBG .removeEventListener(NxWindowEvent.WINDOW_CLOSE,                onClickClose);
			ui.nextBtn	.removeEventListener(MouseEvent.CLICK,							onClickNext);
			ui.addBtn	.removeEventListener(MouseEvent.CLICK,							onClickAdd);
			ui.sendBtn  .removeEventListener(MouseEvent.CLICK,                          onClicksend);
			ui.picBtn   .removeEventListener(MouseEvent.MOUSE_DOWN,                     onPicBox);
			
			ui.inputTxt1.removeEventListener(Event.CHANGE,								onChange);
			
			ui.inputTxt1.removeEventListener(MouseEvent.CLICK,								onClickInput);
			ui.addblackBtn.removeEventListener(MouseEvent.CLICK,						onClickAddBlack);
			//			ui.lookBtn	.removeEventListener(MouseEvent.CLICK,							onClikckLook);
			StageInfo.stage.removeEventListener(KeyboardEvent.KEY_DOWN,                 onEnter);
			ui.minBtn.removeEventListener(MouseEvent.CLICK,onMinHandle);
		}
		private function onMinHandle(e:MouseEvent):void
		{
			BottomIndexLogic.getInstance().showWhisperInfo(_currCahtVo,1);
			ui.hide();
		}
		private  var inputT:TextField
		/**
		 * viewBox(表情)回调函数 
		 * @param argc
		 * 
		 */		
		public  function viewBoxFunction(...argc):void
		{
			
			if(ui.inputTxt1.text.length > ui.inputTxt1.maxChars - argc[0].length)
			{
				return;
			}
			
			ui.inputTxt1.replaceText(ui.inputTxt1.caretIndex ,ui.inputTxt1.caretIndex ,argc[0]);
			_viewBox.hide();
		}
		public function updateUI():void
		{
			
			ui.windowBG.title       = StaticIni.getSwfLangStrVar("UIString_1000104",[_currCahtVo.name]);
			ui.nameLabel.text       = _currCahtVo.name;
			ui.levelLabel.text		= StaticIni.getSwfLangStrVar("UIString_1000036",[_currCahtVo.level]);
			ui.picClip.source		= ResourcesPath.getMiniHeadPic(_currCahtVo.modeId,_currCahtVo.clotheModel == 0 ? 1 : _currCahtVo.clotheModel);
			BottomIndexLogic.getInstance().showWhisperInfo(_currCahtVo,2);
			if(!isOtherMsg &&!isHaveMsg)
			{
				ui.nextBtn.enabled =false;
				BottomIndexLogic.getInstance().FriendTxBtn=false;
			}
			else
			{
				ui.nextBtn.enabled =true;
			}
			
			
		}
		/**
		 * 显示一条数据 
		 * @param arr 消息数据 arr[0]发送者的姓名 arr[3]消息内容
		 
		 * 
		 */		 
		public function showItemWhisper(arr:Array):void
		{
			var data:Array=arr;
			var name:String =data[0];
			var time:String =getTimeStr(data[2]);
			var str:String  =data[3]; 
			var whisper:String =StaticIni.getSwfLangStrVar("UIString_1000110",[name,time]);
			var showStr:String = getUnimbattleStr(str);
			if(data != null)
			{
				ui.chatText.insterTalkStr1(whisper);
				ui.chatText.insterTalkStr1(showStr)
			}
			function getUnimbattleStr(name:String):String
			{
				return TipsMainLogic.instance.getColorHtml(name,"ffffff");
			}
			
		}
		/**
		 * 获取离线信息第一个用户信息 
		 * @return 
		 * 
		 */		
		public function getObjecInfo(index:int):FriendVo
		{
			var nameArr:Array =getMsgName();
			_msgNameArr = nameArr;
			if(nameArr.length<1)
			{
				return new FriendVo(null,0,0,0,0,0);
			}
			if(index>=nameArr.length)
			{
				index=nameArr.length-1;
			}
			var name:String = nameArr[index];
			for(var i:int=0;i<_noReadMsgList.length;i++)
			{
				if(_noReadMsgList[i][0]==name)
				{
					return new FriendVo(name,_noReadMsgList[i][6],0,_noReadMsgList[i][5],_noReadMsgList[i][4],_noReadMsgList[i][7]);
				}
			}
			return new FriendVo(null,0,0,0,0);
		}
		/**
		 * 获取第一个在线未读玩家消息 
		 * @param index
		 * @return 
		 * 
		 */		
		public function getOnlinObjecInfo(index:int):FriendVo
		{
			var nameArr:Array =getOnlineMsgName()
			_onlinmsgNameArr = nameArr;
			if(nameArr.length<1)
			{
				return new FriendVo(null,0,0,0,0,0);
			}
			if(index>=nameArr.length)
			{
				index=nameArr.length-1;
			}
			var name:String = nameArr[index];
			for(var i:int=0;i<_noReadOnLion.length;i++)
			{
				if(_noReadOnLion[i][0]==name)
				{
					return new FriendVo(name,_noReadOnLion[i][6],0,_noReadOnLion[i][5],_noReadOnLion[i][4],_noReadOnLion[i][7]);
				}
			}
			return new FriendVo(null,0,0,0,0,0);
		}
		/**
		 * 设置当前私聊的玩家 
		 * @param str
		 * 
		 */		
		public function set Whispername(vo:FriendVo):void
		{
			_currCahtVo = vo;	
			ui.chatText.clearTalkText();
			//			ui.chatText.setSizeWH(ui.outputArea.width-25,ui.outputArea.height-10);
			byNameReadAallReadMsg(_currCahtVo.name); //先添加记录
			//			checkHaveMsg();
			//			by
			if(isHaveMsg)
			{
				//先检测离线消息中有没有该玩家消息
				var msg:Array= byNameCkeckMsg(_currCahtVo.name);
				for(var i:int=0;i<msg.length;i++)
				{
					showItemWhisper(msg[i]);
				}
				deleNoReadMsg(_currCahtVo.name);
				
			}
			else if(isOtherMsg)
			{
				//再检测在线未读的消息中有没有该玩家的
				var msg1:Array= byNameCkeckOnlineMsg(_currCahtVo.name);
				for(var ii:int=0;ii<msg1.length;ii++)
				{
					showItemWhisper(msg1[ii]);
				}
				deleNoReadOnlinMsg(_currCahtVo.name);
			}
		}
		/**
		 * 获取当前私聊的玩家 
		 * @return 
		 * 
		 */		
		public function get Whispername():FriendVo
		{
			return _currCahtVo;
		}
		/**
		 *根据名字删除未读的在线消息 
		 * @param name
		 * 
		 */		
		private function deleNoReadOnlinMsg(name:String):void
		{
			var arr:Array=[];
			for(var i:int=_noReadOnLion.length-1;i>=0;i--)
			{
				if(_noReadOnLion[i][0]==name)
				{
					//先保存在已读数组中
					//					saveReadChatMsg(_noReadOnLion[i]);
					arr.unshift(_noReadOnLion[i])
					_noReadOnLion.splice(i,1);
				}
			}
			for (var j:int = 0; j < arr.length; j++) 
			{
				saveReadChatMsg(arr[j]);
			}
			if(_noReadOnLion.length ==0)
			{
				isOtherMsg =false;
			}
		}
		/**
		 *根据名字删除已读的离线消息 
		 * @param name
		 * 
		 */		
		private function deleNoReadMsg(name:String):void
		{
			var arr:Array=[];
			for(var i:int=_noReadMsgList.length-1;i>=0;i--)
			{
				if(_noReadMsgList[i][0]==name)
				{
					//先保存在已读数组中
					//					saveReadChatMsg();
					arr.unshift(_noReadMsgList[i]);
					//在从离线消息中删除已读的
					_noReadMsgList.splice(i,1);
					
				}
			}
			for (var j:int = 0; j < arr.length; j++) 
			{
				saveReadChatMsg(arr[j]);
			}
			if(_noReadMsgList.length == 0)
			{
				isHaveMsg =false;
			}
		}
		
		/**
		 *发送聊天信息 
		 * 
		 */		 
		private function sendMsg(isHtml: Boolean):void
		{
			var message:String = ui.inputTxt1.text;
			if(message=="") return;
			
			if(isHtml)
			{
				message = htmlCode(message);
			}
			
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_WHISPER,new WString(_currCahtVo.name), new WString(message));
			
			if(FriendLogic.getInstance().byNameckeckBlack(_currCahtVo.name))
			{
				return;
			}
			else
			{
				ui.textLabel.htmlText =StaticIni.getSwfLangStrVar("UIString_1000109",[ui.inputTxt1.maxChars]);
			}
			var time:Number = getNowTimer();
			//			showItemWhisper([_model.scene.Role.getPlayerName(),time,message]);
			//发送后清空输入框
			if(message.indexOf("/") != 0)
			{
				ui.inputTxt1.text = "";
			}
			else
			{
				ui.inputTxt1.text	=	message.substr(0, message.indexOf(" ") + 1);
			}
			
		}
		/**
		 *	保存已读聊天消息 
		 * @param message		聊天信息
		 * 
		 */
		private function saveReadChatMsg(message : Array):void
		{
			//判断是否有重复
			for(var i : int = 0; i < _readMsgList.length; i++)
			{
				if(_readMsgList[i] == message)
				{
					_readMsgList.splice(i, 0);
					break;
				}
			}
			_readMsgList.push(message);		 
		}
		/**
		 * 保存未读离线聊天消息 
		 * @param messag
		 * 
		 */		
		private function saveNoReadChatMsg(messag :Array):void
		{
			//判断是否有重复
			for(var i : int = 0; i < _noReadMsgList.length; i++)
			{
				if(_noReadMsgList[i] == messag)
				{
					_noReadMsgList.splice(i, 0);
					break;
				}
			}
			
			_noReadMsgList.push(messag);
			taxisFunc(_noReadMsgList);
		}
		/**
		 * 保存未读的在线消息 
		 * @param messag
		 * 
		 */		
		private function saveNoReadOnlinMsg(messag:Array):void
		{
			//判断是否有重复
			for(var i : int = 0; i < _noReadOnLion.length; i++)
			{
				if(_noReadOnLion[i] == messag)
				{
					_noReadOnLion.splice(i, 0);
					break;
				}
			}
			_noReadOnLion.push(messag);
			taxisFunc(_noReadOnLion);
		}
		/**
		 * 监听是否有离线消息 
		 * @param e
		 * 
		 */		
		private function getIsHaveMsg(e:CustomMsgEvent):void
		{
			isHaveMsg = true;
			BottomIndexLogic.getInstance().FriendTxBtn=true;
			//			BottomIndexLogic.getInstance().a.startGlowHandler(BottomIndexLogic.getInstance()._buddyBtn);
		}
		/**
		 *是否有离线 
		 * @return 
		 * 
		 */		
		public function get isHaveNoLineMsg():Boolean
		{
			return isHaveMsg;
		}
		/**
		 * 获得服务器返回聊天消息 
		 * @param e
		 * 接受数据的格式 
		 */
		private function getMsg(e:CustomMsgEvent):void
		{
			var dataArr:Array = e.pList;
			var type:int=dataArr.shift();  // 1及时消息  2离线消息
			
			for(var i:int=0;i<dataArr.length;i+=9)
			{
				var data : Array      = [];
				//消息发送者的名字
				var chatname:String   = dataArr[i];
				//发送的对象
				var seename:String	  = dataArr[i+1];
				//消息时间
				var chattime:Number	  = dataArr[i+2]*1000;
				//发送的消息
				var chatstr :String   = dataArr[i+3];
				//模型号
				var modelID:int		= dataArr[i+4]
				//等级
				var level:int		= dataArr[i+5];
				//在线状态
				var state:int		= dataArr[i+6];
				//性别
				var sex:int			= dataArr[i+7];
				//服装号
				var clotheModel:int =dataArr[i+8];
				//过滤不能显示的文章
				var glStr:Array       = StaticIni.getIniVar("filter_list","data").split(",");
				if(chatstr.indexOf("<") == -1)
				{
					chatstr	=	ChatFilterLogic.getInstance().filter(chatstr);
				}
				if(FriendLogic.getInstance().byNameckeckBlack(chatname)) continue;
				data =[chatname,seename,chattime,chatstr,modelID,level,state,clotheModel];
				if(chatname != _model.scene.Role.getPlayerName())
				{
					//添加最近联系人
					var infoArr:Array =[chatname,state,sex,level,modelID,clotheModel];
					var vo:FriendVo = FriendLogic.getInstance().getOneUser(infoArr,3);
					FriendLogic.getInstance().saveTemporaryUser(vo);
				}
				
				//如果是自己发的 或者是聊天对象发的
				if(ui.created)
				{
					if(_currCahtVo.name ==chatname || chatname == _model.scene.Role.getPlayerName())
					{
						showItemWhisper(data);
						saveReadChatMsg(data);
						
						return;
					}
					else
					{
						//当前聊天的玩家不是该玩家保存在未读在线数组中
						saveNoReadOnlinMsg(data);
						isOtherMsg=true;
						updateUI();
						BottomIndexLogic.getInstance().FriendTxBtn=true;
						//						BottomIndexLogic.getInstance().a.startGlowHandler(BottomIndexLogic.getInstance()._buddyBtn);
					}
				}
				else if(type ==2)
				{
					//离线消息
					saveNoReadChatMsg(data);
				}
				else if(type == 1)
				{
					//在未打开
					saveNoReadOnlinMsg(data);
					isOtherMsg=true;
					BottomIndexLogic.getInstance().FriendTxBtn=true;
					//					BottomIndexLogic.getInstance().a.startGlowHandler(BottomIndexLogic.getInstance()._buddyBtn);
				}
				
			}
			//有离线消息 或者有未读消息
			if(type == 2)
			{
				//设置当前查看玩家
				_currIndex=0;
				_currCahtVo = getObjecInfo(_currIndex);
				if(_currCahtVo.name ==null)
					return;
				byNameReadNexMsg(_currCahtVo.name); //先读离线消息
				byNameReadOnlinMsg(_currCahtVo.name);//在读在线消息
				ui.show();
			}
			else
			{
				if(isOtherMsg)
				{
					//					_currIndex=0;
					//					_currCahtVo = getOnlinObjecInfo(_currIndex);
					
				}
				
			}
			
		}
		/**
		 *只要打开私聊界面必须掉这个 
		 * @return 
		 * 
		 */		
		public function checkHaveMsg():void
		{
			if(isHaveMsg)
			{
				//有离线MSG   这时候不需要显示聊天记录
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ACCEPT_OFFLINE_WHISPER);
				BottomIndexLogic.getInstance().FriendTxBtn=false;
			}
			else if(isOtherMsg)
			{
				
				//界面关闭着
				if(!ui.created)
				{
					seeNewMsg();
					reaOtherdMsg();	
					BottomIndexLogic.getInstance().FriendTxBtn=false;
				}
				else
				{
					lookNextMsg();
				}
			}
			
		}
		public function reaOtherdMsg():void
		{
			byNameReadOnlinMsg(_currCahtVo.name);//在读在线消息
		}
		/**
		 * 打开私聊时检查该玩家有没有未读消息  有的话显示 
		 * @param name
		 * 
		 */		
		private function checkCurrNoReadMsg():void
		{
			for(var i:int =0;i<_noReadMsgList.length;i++)
			{
				if(_currCahtVo.name ==_noReadMsgList[i][1] )
				{
					showItemWhisper(_noReadMsgList[i]);
					_noReadMsgList.splice(i,1);
				}
			}
		}
		/**
		 * 清除聊天信息  
		 * 
		 */
		private function clearMsg():void
		{
			
			ui.chatText.clearTalkText();
			//			ui.chatText.setSizeWH(ui.outputArea.width-25,ui.outputArea.height-10);
			_currCahtVo = new FriendVo("",0,0,0,0);
			
		}
		
		private function htmlCode(content : String) : String
		{
			var msg : String = content;
			var myPattern:RegExp = new RegExp("<","g");  
			var myPattern1:RegExp = new RegExp(">","g");  
			
			msg = msg.replace(myPattern,"&lt;");
			msg = msg.replace(myPattern1,"&gt;");
			return msg;
		}
		/**
		 * 读取改用户所有离线消息 
		 * @param name
		 * 
		 */		
		private function byNameReadNexMsg(name:String):void
		{
			var msg:Array=byNameCkeckMsg(name);
			if(msg==null)
				return;
			for(var i:int=0;i<msg.length;i++)
			{
				showItemWhisper(msg[i]);
				deleNoReadMsg(msg[i][0]); //删除已读的离线消息
			}
		}
		/**
		 * 读取改用户所有未读的在线消息 
		 * @param name
		 * 
		 */		
		private function byNameReadOnlinMsg(name:String):void
		{
			//			byNameReadAallReadMsg(name);
			var msg:Array=byNameCkeckOnlineMsg(name);
			if(msg==null)
				return;
			for(var i:int=0;i<msg.length;i++)
			{
				showItemWhisper(msg[i]);
				deleNoReadOnlinMsg(msg[i][0]);//删除已读的在线消息
			}
		}
		/**
		 *显示该用户所有的聊天记录 
		 * @param name
		 * 
		 */		
		private function byNameReadAallReadMsg(name:String):void
		{
			var msg:Array = byNameCkeckReadMsg(name);
			if(msg ==null)return;
			for(var i:int=0;i<msg.length;i++)
			{
				showItemWhisper(msg[i]);
			}
		}
		/**
		 * 获取离线名字数组
		 * @return 
		 * 
		 */		
		private function getMsgName():Array
		{
			var nameArr:Array=[];
			if(_noReadMsgList.length>0)
			{
				for(var jj:int=0;jj<_noReadMsgList.length;jj++)
				{
					if(nameArr.indexOf(_noReadMsgList[jj][0])==-1)
						nameArr.push(_noReadMsgList[jj][0]);
				}
				return nameArr;
			}
			else
			{
				return [];
			}
		}
		/**
		 * 获取在线线名字数组
		 * @return 
		 * 
		 */		
		private function getOnlineMsgName():Array
		{
			var nameArr:Array=[];
			if(_noReadOnLion.length>0)
			{
				
				for(var jj:int=0;jj<_noReadOnLion.length;jj++)
				{
					if(nameArr.indexOf(_noReadOnLion[jj][0])==-1)
						nameArr.push(_noReadOnLion[jj][0]);
				}
				return nameArr;
			}
			else
			{
				return [];
			}
		}
		/**
		 *返回该玩家所有离线消息 
		 * @param name
		 * @return 
		 * 
		 */		
		private function byNameCkeckMsg(name:String):Array
		{
			var msgArr:Array=[];
			for(var i:int=0;i<_noReadMsgList.length;i++)
			{
				if(_noReadMsgList[i][0] == name)
				{
					msgArr.push(_noReadMsgList[i]);
				}
			}
			taxisFunc(msgArr);
			return msgArr;
		}
		/**
		 * 返回该玩家所有的未读在线消息 
		 * @param name
		 * @return 
		 * 
		 */		
		private function byNameCkeckOnlineMsg(name:String):Array
		{
			var msgArr:Array=[];
			for(var i:int=0;i<_noReadOnLion.length;i++)
			{
				if(_noReadOnLion[i][0] == name)
				{
					msgArr.push(_noReadOnLion[i]);
				}
			}
			taxisFunc(msgArr);
			return msgArr;
		}
		/**
		 * 返回该玩家所有的已读消息 
		 * @param name
		 * @return 
		 * 
		 */		
		private function byNameCkeckReadMsg(name:String):Array
		{
			var msgArr:Array=[];
			for(var i:int=0;i<_readMsgList.length;i++)
			{
				if(_readMsgList[i][0] == name || _readMsgList[i][1] ==name)
				{
					msgArr.push(_readMsgList[i]);
				}
			}
			taxisFunc(msgArr);
			return msgArr;
		}
		/**
		 * 根据MSG时间排序 
		 * 
		 */		
		private function taxisFunc(arr:Array):void
		{
			arr = arr.sort(byTimeSort);
		}
		private function byTimeSort(v1:Array,v2:Array):int
		{
			return v1[2]-v2[2];
		}
		/**
		 * 获取当前时间 
		 * @return 
		 * 
		 */		
		private function getNowTimer():Number
		{
			_nowdate =new Date();
			var hours:Number		=_nowdate.hours;
			var minutes:Number		=_nowdate.minutes;
			var seconds:Number		=_nowdate.seconds;
			
			return _nowdate.time;
		}
		/**
		 * 转换时间 
		 * @param _date
		 * @return 
		 * 
		 */		
		private function getTimeStr(date:Number):String
		{
			_date.time =date;
			var hours:Number		=_date.getHours();
			var minutes:Number		=_date.getMinutes();
			var seconds:Number		=_date.getSeconds();
			var str:String  = initTime(hours)+":"+initTime(minutes)+":"+initTime(seconds);
			return str;	
		}
		/**
		 * 格式时间 
		 * @param val
		 * @return 
		 * 
		 */		
		private function initTime(val:Number):String
		{
			var  va:String = val.toString();
			val>9?va:va="0"+va;
			return va;
		}
		private function whisperShow(e:CustomMsgEvent):void
		{
			var name:String	= e.pList[0];
			var state:int	= e.pList[1];
			var sex	:int 	= e.pList[2];
			var level:int	= e.pList[3];
			var modelID:int	= e.pList[4];
			var clotheModel:int=e.pList[5];
			var infoarr:Array = [name,state,sex,level,modelID,clotheModel];
			var vo:FriendVo =FriendLogic.getInstance().getOneUser(infoarr,3);
			FriendLogic.getInstance().WhispChat(vo);
		}
		/**
		 * 请求私聊 
		 * @param name
		 * 
		 */	
		public function whisperSart(name:String):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_CHAT_PLAYER_INFO, new WString(name));	
		}
		
		//////////////////////////////////////////监听函数//////////////////////////////////////////////
		
		private function showUI(e:Event):void
		{
			ui.showOrHide();
		}
		/**
		 * 快捷键
		 * 回车显示输入框，有文字发送，没文字关闭输入 
		 * @param e
		 * 
		 */
		private function onEnter(e:KeyboardEvent):void
		{
			if(enterobj ==null) return;
			if(e.keyCode	==	Keyboard.ENTER)
			{
				if(enterobj ==null ||enterobj !=ui.inputTxt1) return;
				if(ui.inputTxt1.text!="")
				{
					sendMsg(true);
				}
				StageInfo.stage.focus	=	ui.inputTxt1;
			}
			
		}
		/**
		 * 点击关闭 
		 * @param e
		 * 
		 */	    
		private function onClickClose(e:NxWindowEvent):void
		{
			BottomIndexLogic.getInstance().showWhisperInfo(_currCahtVo,2);
			clearMsg();
			ui.hide();
			//			_noReadMsgList =[];
			//			_noReadOnLion  =[];
			//			_onlinmsgNameArr =[];
			//			_msgNameArr		 =[];
			if(_noReadOnLion.length ==0)
			{
				isOtherMsg =false;
			}
			
		}
		/**
		 * 点击发送 
		 * @param e
		 * 
		 */			
		private function onClicksend(e:MouseEvent):void
		{
			if(ui.inputTxt1.text!="")
			{
				sendMsg(true);
			}
			enterobj = ui.inputTxt1;
		}
		private function onClickNext(e:MouseEvent):void
		{
			//有离线消息
			if(isHaveMsg)
			{
				clearMsg();
				var vo:FriendVo =getObjecInfo(_currIndex+1);
				if(_currCahtVo ==null)
				{
					return;
				}
				_currIndex++;
				_currCahtVo =vo;
				
				byNameReadNexMsg(_currCahtVo.name);
				byNameReadOnlinMsg(_currCahtVo.name);
				updateUI();
			}
			else if(isOtherMsg)
			{
				lookNextMsg();
			}
			
		}
		/**读一条新数据 打开界面**/
		public function seeNewMsg():void
		{
			//			if(_onlinmsgNameArr.length>1)
			//			{
			//				ui.nextBtn.enabled =true;
			//			}
			//			else
			//			{
			//				ui.nextBtn.enabled =false;
			//			}
			
			_currCahtVo=getOnlinObjecInfo(0);
			if(_currCahtVo ==null) return;
			byNameReadAallReadMsg(_currCahtVo.name)
			byNameReadOnlinMsg(_currCahtVo.name);
			
			ui.show();
		}
		//查看下一个人的消息
		public function lookNextMsg():void
		{
			clearMsg();
			var vo1:FriendVo =getOnlinObjecInfo(_currIndex+1);
			if(_currCahtVo ==null)
			{
				return;
			}
			_currIndex++;
			_currCahtVo =vo1;
			
			byNameReadOnlinMsg(_currCahtVo.name);
			updateUI();
		}
		private function onClickAdd(e:MouseEvent):void
		{
			FriendLogic.getInstance().sendAddFriend(_currCahtVo.name);
		}
		/**
		 * 按下表情按钮 
		 * @param e
		 * 
		 */		
		private function onPicBox(e:MouseEvent):void
		{
			
			if(this._viewBox.created)
			{
				_viewBox.removeStageEvent();
				_viewBox.hide();
			}
			else
			{
				_viewBox.showBtn = e.target as DisplayObject;
				_viewBox.addStageEvent();	
				_viewBox.show();
				_viewBox.x=	ui.windowBG.x + 330
				_viewBox.y=	ui.windowBG.y+ui.height/2;
			}
		}
		private function onClickAddBlack(e:MouseEvent):void
		{
			FriendLogic.getInstance().addBlackFunc(_currCahtVo.name);
			
		}
		private function updateFriend():void
		{
			var record:Record = _model.scene.GetRecordEx("Friend");
			var isFriend:Boolean = check(_currCahtVo.name,record);
			if(!isFriend)
			{
				ui.addBtn.enabled =true;
			}
			else
			{
				ui.addBtn.enabled =false;
			}
		}
		private function updateBlack():void
		{
			var record:Record = _model.scene.GetRecordEx("BlackList");
			var isBlack:Boolean = check(_currCahtVo.name,record);
			if(!isBlack)
			{
				ui.addblackBtn.enabled =true;
				ui.label4.filters =[];
				ui.mc4.enabled=true;
			}
			else
			{
				ui.addblackBtn.enabled =false;
				ui.label4.filters =[FilterUtil.GrayColorFilter];
				ui.mc4.enabled=false;
			}
		}
		private function onClikckLook(e:MouseEvent):void
		{
			ActorPropertyLogic.getInstance().lookOver(_currCahtVo.name,moveMC);
		}
		private function moveMC():void
		{
			ui.windowBG.x = 430-ui.windowBG.width/2;
			_acterui.windowBG.x =370+_acterui.windowBG.width/2;
			var posx1:Number =370-_acterui.windowBG.width/2;
			var posx2:Number =350+ui.windowBG.width/2;
			TweenLite.to(ui.windowBG,0.5,{x:posx2});
			TweenLite.to(_acterui.windowBG,0.5,{x:posx1});
		}
		private function onClickInput(e:MouseEvent):void
		{
			if(TopOtherPlayerLogic.getInstance().ui.created)
			{
				TopOtherPlayerLogic.getInstance().ui.hide();
			}
			enterobj=ui.inputTxt1;
			StageInfo.stage.focus	=	ui.inputTxt1;
			
		}
		private function onChange(e:Event):void
		{
			//			enterobj=ui.inputTxt1;
			var val:int =ui.inputTxt1.maxChars-ui.inputTxt1.text.length;
			ui.textLabel.htmlText =StaticIni.getSwfLangStrVar("UIString_1000109",[val]);
		}
	}
}